// components/collect/collect-component.js
const util = require('../../utils/util.js')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
		page: {
      type: String,
			value:""
    },
		refresh:{
			type:Boolean
		}
  },
	ready: function() {
		this.obainCollect();
	},
  /**
   * 组件的初始数据
   */
  data: {
		dynamic:[],
		noneData:false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
		/* 获取收藏 */
		obainCollect:function(){
			var _this=this;
			wx.request({
				url: util.url+"/usi/queryFavorites",
				method:"post",
				data: {
					"articleId":-1,
					"storeBy":JSON.parse(wx.getStorageSync("userInfo")).openId,
					"page":_this.data.page,
					"size":20
				},
				header: {
					'content-type': 'application/json' 
				},
				success(res) {
					wx.hideLoading();
					if(res.data.resultCode==0){
						if(_this.data.refresh){
							wx.hideNavigationBarLoading() //完成停止加载
							wx.stopPullDownRefresh() //停止下拉刷新
						}
						var userStoreInfoList=res.data.resultData.userStoreInfoList;
						if(res.data.resultData.totalCount){
							_this.setData({
								noneData:false
							})
						}else{
							_this.setData({
								noneData:true
							});
						}
						var hasData=false;
						for(var i=0;i<userStoreInfoList.length;i++){
							if(!userStoreInfoList[i].article_cover_url){
								userStoreInfoList[i].article_cover_url="";
								userStoreInfoList[i].addClass="all-width";
							}
							userStoreInfoList[i].createAt=userStoreInfoList[i].createAt?util.formatTime(new Date(userStoreInfoList[i].createAt)):"";
							userStoreInfoList[i].fileDate=userStoreInfoList[i].fileDate?util.formatTime(new Date(userStoreInfoList[i].fileDate)):"";
							if(userStoreInfoList[i].article_id!=-1){
								hasData=true;
							}
						}
						_this.data.page+=1;
						if(userStoreInfoList.length&&hasData){
							_this.setData({
								dynamic:userStoreInfoList,
							})
						}
						_this.data.refresh=false;
					}else{
						wx.showToast({
							icon:"none",
							title:res.data.resultMsg
						})
					}
				},
				fail: res => {
					wx.hideLoading();
					wx.showToast({
						title:"获取数据失败"
					})
				}
			});
		},
		/* 跳转到详情页 */
		toDetails:function(e){
			if(this.data.currIndex==-1){
				if(e.currentTarget.dataset.status=='正常'){
					if(e.currentTarget.dataset.articletype==11){
						wx.navigateTo({
							url: "../articleDetail/articleDetail?judgeType=unscramble&id="+e.currentTarget.dataset.articleid
						})
					}else if(e.currentTarget.dataset.articletype==13){
						wx.navigateTo({
							url: "../articleDetail/articleDetail?judgeType=manuscript&id="+e.currentTarget.dataset.articleid
						})
					}else if(e.currentTarget.dataset.articletype==14){
						wx.navigateTo({
							url: "../articleDetail/articleDetail?judgeType=&id="+e.currentTarget.dataset.articleid
						})
					}
				}else if(e.currentTarget.dataset.status=='更新'){
					var _this=this;
					wx.request({
						url: util.url+"/usi/forcedUpdate",
						method:"post",
						data: {
							"id":e.currentTarget.dataset.id,
							"articleId":e.currentTarget.dataset.articleid,
						},
						header: {
							'content-type': 'application/json' 
						},
						success(res) {
							if(res.data.resultCode==1){
								var resultData={
									article_content:res.data.resultData.content,
									article_cover_url:res.data.resultData.coverUrl,
									article_id:res.data.resultData.id,
									article_title:res.data.resultData.title,
									article_type:res.data.resultData.type,
									file_date:res.data.resultData.fileDate,
									id:_this.data.dynamic[e.currentTarget.dataset.index].id,
									status:"正常",
								}
								_this.data.dynamic[e.currentTarget.dataset.index]=resultData;
								_this.setData({
									dynamic:_this.data.dynamic
								});
								wx.showToast({
									icon:"none",
									title:"本条收藏已同步更新！",
								});
								var timer=setTimeout(function(){
									clearTimeout(timer);
									if(e.currentTarget.dataset.articletype==11){
										wx.navigateTo({
											url: "../articleDetail/articleDetail?judgeType=unscramble&id="+e.currentTarget.dataset.articleid
										})
									}else if(e.currentTarget.dataset.articletype==13){
										wx.navigateTo({
											url: "../articleDetail/articleDetail?judgeType=manuscript&id="+e.currentTarget.dataset.articleid
										})
									}else if(e.currentTarget.dataset.articletype==14){
										wx.navigateTo({
											url: "../articleDetail/articleDetail?judgeType=&id="+e.currentTarget.dataset.articleid
										})
									}
								},1500)
								
							}else{
								wx.showToast({
									icon:"none",
									title:res.data.resultMsg,
								})
							}
						}
					})
				}else{
					wx.showToast({
						icon:"none",
						title:"当前收藏已被删除！",
					})
				}
			}else{
				this.setData({
					currIndex:-1,
				})
			}
		},
		/* 长按取消收藏 */
		longPress:function(e){
			this.setData({
				currIndex:e.currentTarget.dataset.index,
			})
		},
		/* 取消该条收藏数据 */
		deleteFn:function(e){
			var _this=this;
			wx.request({
				url: util.url+"/usi/saveOrUpload",
				method:"post",
				data: {
					"id":e.currentTarget.dataset.id,
					"articleId":-1,
					"storeBy":JSON.parse(wx.getStorageSync("userInfo")).openId,	
				},
				header: {
					'content-type': 'application/json' 
				},
				success(res) {
					if(res.data.resultCode==0){
						_this.data.dynamic.splice(_this.data.currIndex,1);
						_this.setData({
							dynamic:_this.data.dynamic,
							currIndex:-1,
						})
						wx.showToast({
							icon:"none",
							title:"取消收藏成功",
						})
						if(_this.data.dynamic.length){
							_this.setData({
								noneData:false
							})
						}else{
							_this.setData({
								noneData:true
							});
							
						}
					}else{
						wx.showToast({
							icon:"none",
							title:res.data.resultMsg,
						})
					}
				}
			})
			
		},
  }
})
